package com.cocoiko.customview;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cocoiko.info.CocoikoInfo;

public class CocoikoListAdapter extends ArrayAdapter<CocoikoInfo> {

	Context context;
	int resource;
	ArrayList<CocoikoInfo> array;

	public CocoikoListAdapter(Context context, int textViewResourceId,
			ArrayList<CocoikoInfo> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.resource = textViewResourceId;
		this.array = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		View view = convertView;
		if (view == null) {
			view = new CocoikoListItem(context,null);
		}
		CocoikoInfo info = array.get(position);

		if (info != null) {
			ImageView avatar = ((CocoikoListItem) view).avatar;
			TextView comment = ((CocoikoListItem) view).comment;
			ImageView shop_icon = ((CocoikoListItem) view).shop_icon;
			TextView shop_name = ((CocoikoListItem) view).shop_name;
			TextView shop_address = ((CocoikoListItem) view).shop_address;
			GridView lead_avatar = ((CocoikoListItem) view).lead_avatar;
			TextView lead_number = ((CocoikoListItem) view).lead_number;
			TextView time = ((CocoikoListItem) view).time;

			if (info.getAvatar() != null)
				avatar.setImageBitmap(info.getAvatar());
			if (info.getComment() != null)
				comment.setText(info.getComment());
			if (info.getShop_icon() != null)
				shop_icon.setImageBitmap(info.getShop_icon());
			if (info.getShop_name() != null)
				shop_name.setText(info.getShop_name());
			if (info.getShop_address() != null)
				shop_address.setText(info.getShop_address());
			if (info.getShop_icon() != null) {
				lead_number.setText(String
						.valueOf(info.getLead_avatar().size())
						+ "���");
				lead_avatar.setAdapter(new GridAdapter(context, info
						.getLead_avatar()));
			} else {
				LinearLayout lead = ((CocoikoListItem) view).lead;
				LayoutParams params = lead.getLayoutParams();
				params.height = 0;
				lead.setLayoutParams(params);
			}
			if (info.getTime() != null)
				time.setText(info.getTime());
		}

		return view;
	}
}
